

package com.maoshi.shop.multishop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoshi.shop.bean.model.ShopBankCard;
import com.maoshi.shop.common.annotation.SysLog;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.security.multishop.util.SecurityUtils;
import com.maoshi.shop.service.ShopBankCardService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;


/**
 * 商家提现申请信息
 *
 * @author YXF
 *  * @date 2020-04-07 14:22:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopBankCard" )
public class ShopBankCardController {

    private final ShopBankCardService shopBankCardService;



    @GetMapping("/getShopBankCardList")
    public ResponseEntity< List<ShopBankCard>> getShopInfo() {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<ShopBankCard> list = shopBankCardService.list(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopId));
        return ResponseEntity.ok(list);
    }

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody @Valid ShopBankCard shopBankCard) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        shopBankCard.setShopId(shopId);
        shopBankCardService.insert(shopBankCard);
        return ResponseEntity.ok().build();
    }

    /**
     * 设置默认银行卡
     */
    @PutMapping("/setDefault")
    public ResponseEntity<Void> setDefault(@RequestBody @Valid ShopBankCard shopBankCard) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        shopBankCardService.setDefault(shopBankCard.getShopBankCardId(),shopId);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @SysLog("删除公告管理")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> removeById(@PathVariable Long shopBankCardId) {
        ShopBankCard shopBankCard = shopBankCardService.getById(shopBankCardId);
        if (Objects.equals(shopBankCard,1)){
            throw new MaoshiShopBindException("不能删除默认银行卡");
        }
        shopBankCardService.removeById(shopBankCardId);
        return ResponseEntity.ok().build();
    }


}
